﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Delay;
using System.ComponentModel;
using System.Windows.Controls.Primitives;
using System.Diagnostics;
using System.Collections.ObjectModel;
using Microsoft.Phone.Shell;

namespace PhoneInfo.Page
{
    public partial class HotPhone : PhoneApplicationPage
    {
        private string classid;
        private string ppid;
        private SearchResultView hotResult = null;
        private SearchResultView newResult = null;
        private SearchResultView cheapResult = null;
        private SearchResultView dearResult = null;
        private SearchResultView scoreResult = null;
        //
        private SearchResultView hotResultType = null;
        private SearchResultView newResultType = null;
        private SearchResultView cheapResultType = null;
        private SearchResultView dearResultType = null;
        private SearchResultView scoreResultType = null;
        //
        private ObservableCollection<string> hotResultTypeKyes = null;
        private ObservableCollection<string> newResultTypeKyes = null;
        private ObservableCollection<string> cheapResultTypeKyes = null;
        private ObservableCollection<string> dearResultTypeKyes = null;
        private ObservableCollection<string> scoreResultTypeKyes = null;

        bool ispk = false;
        bool isnull = false;
        string pid_1;
        string ppid_1;
        string price_1;
        string picurl_1;
        string phonename_1;
        string ppname_1;

        private int lastPage = -1;
        private string keychar = "";
        int type = -1;//1,是从热门机型排行下来的 2.是从欧美 日韩 国产 台湾 这4个项目下来的
        public HotPhone()
        {
            InitializeComponent();
        }
        void type_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (sender == null)
            {
                return;
            }
            SearchResultView search = sender as SearchResultView;
            if (search == null)
            {
                return;
            }

            else if (e.PropertyName == "Result")
            {
                ProgBar.Visibility = Visibility.Collapsed;
                switch (search.Result)
                {
                    case -1:
                    case 1:
                        //一个是失败 一个是服务器抛出异常
                        switch (mainPivot.SelectedIndex)
                        {
                            case 0:

                                NewResult.Visibility = Visibility.Visible;
                                break;
                            case 1:
                                HotResult.Visibility = Visibility.Visible;
                                break;
                            case 2:
                                CheapResult.Visibility = Visibility.Visible;
                                break;
                            case 3:
                                DearResult.Visibility = Visibility.Visible;
                                break;
                            case 4:
                                ScoreResult.Visibility = Visibility.Visible;
                                break;
                        }
                        break;
                    case 2:
                        //成功  当类型是欧美 国产 日韩 国内 台湾 弹出选择面板
                        if (type == 2)
                        {//将key中的内容读出
                            switch (mainPivot.SelectedIndex)
                            {
                                case 0:
                                    newResultTypeKyes = ParseKey(newResultType.Key);
                                    break;
                                case 1:
                                    //
                                    hotResultTypeKyes = ParseKey(hotResultType.Key);
                                    //  Expander.DataContext = newResultTypeKyes;
                                    ;
                                    // Expander.RenderTransform. += Expander.Height;
                                    break;
                                case 2:
                                    //
                                    cheapResultTypeKyes = ParseKey(cheapResultType.Key);
                                    // Expander.DataContext = newResultTypeKyes;
                                    break;
                                case 3:
                                    //
                                    dearResultTypeKyes = ParseKey(dearResultType.Key);
                                    //Expander.DataContext = newResultTypeKyes;
                                    break;
                                case 4:
                                    //
                                    scoreResultTypeKyes = ParseKey(scoreResultType.Key);
                                    //  Expander.DataContext = newResultTypeKyes;
                                    break;
                            }
                        }
                        break;
                }
            }
        }
        private void ListBox_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (sender == null)
            {
                return;
            }
            ListBox lb = sender as ListBox;
            if (lb == null)
            {
                return;
            }

            phonePkItem pkItem = lb.SelectedItem as phonePkItem;
            if (pkItem == null)
            {
                return;
            }

            if (ispk)
            {
                string objective = "/Page/PhonePk.xaml?type=1";
                if (!isnull)
                {
                    objective += "&pid1=" + pid_1;
                    objective += "&name1=" + ppname_1 + "\r\n" + phonename_1;
                    objective += "&phonename1=" + ppname_1;
                    objective += "&ppname1=" + phonename_1;
                    objective += "&ppid=" + ppid_1;
                    objective += "&picurl1=" + picurl_1;
                    objective += "&pid2=" + pkItem.Ponhe1_pid;
                    objective += "&ppid2=" + pkItem.Ponhe1_ppid;
                    objective += "&picurl2=" + pkItem.Ponhe1_picurl;
                    objective += "&name2=" + pkItem.Ponhe1_ppname + "\r\n" + pkItem.Ponhe1_phonename;
                    objective += "&phonename2=" + pkItem.Ponhe1_phonename;
                    objective += "&ppname2=" + pkItem.Ponhe1_ppname;
                }
                else {
                    objective += "&isnull=1";
                    objective += "&pid1=" + pkItem.Ponhe1_pid;
                    objective += "&name1=" + pkItem.Ponhe1_ppname + "\r\n" + pkItem.Ponhe1_phonename;
                    objective += "&phonename1=" + pkItem.Ponhe1_phonename;
                    objective += "&ppname1=" + pkItem.Ponhe1_ppname;
                    objective += "&ppid=" + pkItem.Ponhe1_ppid;
                    objective += "&picurl1=" + pkItem.Ponhe1_picurl;
                }
                
                NavigationService.Navigate(new Uri(objective, UriKind.Relative));
                return;
            }

            if (pkItem.Type == "Main")
            {
                string objective = "/Page/TypeData.xaml";
                objective += "?pid=" + pkItem.Ponhe1_pid;
                objective += "&price=" + pkItem.Ponhe1_price;
                objective += "&maxprice=" + pkItem.Ponhe1_maxprice;
                objective += "&minprice=" + pkItem.Ponhe1_minprice;
                objective += "&picurl=" + pkItem.Ponhe1_picurl;
                objective += "&ppname=" + pkItem.Ponhe1_ppname;
                objective += "&phonename=" + pkItem.Ponhe1_phonename;
                objective += "&ppid=" + pkItem.Ponhe1_ppid;
                NavigationService.Navigate(new Uri(objective, UriKind.Relative));
            }
            else
            {//
                if (App.appSitting.AutoLoadNextPage == false)
                {
                    PivotItem currentPivot = mainPivot.SelectedItem as PivotItem;
                    if (currentPivot == null)
                    {
                        return;
                    }

                    SearchResultView infoView = currentPivot.DataContext as SearchResultView;
                    if (infoView == null)
                    {
                        return;
                    }
                    if (infoView.CurrentPage * infoView.PageSize <= infoView.TotalPage)
                    {
                        //
                        if (lastPage == infoView.CurrentPage)
                        {

                            return;
                        }
                        lastPage = infoView.CurrentPage;
                        Debug.WriteLine("add page name {0} for {1}", infoView.ClassName, infoView.CurrentPage + 1);
                        infoView.AllowAdd = true;
                        if (type == 1)
                        {
                            infoView.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(infoView.GetPhoneInfoUrl(classid, (mainPivot.SelectedIndex + 1).ToString(), infoView.CurrentPage + 1)));
                        }
                        else if (type == 2)
                        {
                            infoView.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(infoView.GetPhoneInfoUrl(ppid, (mainPivot.SelectedIndex + 1).ToString(), infoView.CurrentPage + 1, keychar)));
                        }

                    }
                }


            }


        }
        private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (sender == null) return;
            Pivot pv = sender as Pivot;
            if (pv == null) return;
            if (type == 1)
            {
                switch (pv.SelectedIndex)
                {
                    case 0:
                        lastPage = -1;
                        break;
                    case 1:
                        lastPage = -1;
                        if (hotResult == null)
                        {
                            hotResult = new SearchResultView();
                            hotResult.ClassName = "hotResult";
                            hotResult.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                            hotResult.Result = 0;
                            ProgBar.Visibility = Visibility.Visible;
                            hotResult.CurrentPage = 1;
                            hotResult.TotalPage = 0;
                            hotResult.PageSize = 20;
                            hotResult.AllowAdd = false;
                            hotResult.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(hotResult.GetPhoneInfoUrl(classid, "2", hotResult.CurrentPage)));
                            Hot.DataContext = hotResult;
                        }
                        break;
                    case 2:
                        lastPage = -1;
                        if (cheapResult == null)
                        {
                            cheapResult = new SearchResultView();
                            cheapResult.ClassName = "cheapResult";
                            cheapResult.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                            cheapResult.Result = 0;
                            ProgBar.Visibility = Visibility.Visible;
                            cheapResult.CurrentPage = 1;
                            cheapResult.TotalPage = 0;
                            cheapResult.PageSize = 20;
                            cheapResult.AllowAdd = false;
                            cheapResult.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(cheapResult.GetPhoneInfoUrl(classid, "4", cheapResult.CurrentPage)));
                            Cheap.DataContext = cheapResult;
                        }


                        break;
                    case 3:
                        lastPage = -1;
                        if (dearResult == null)
                        {
                            dearResult = new SearchResultView();
                            dearResult.ClassName = "dearResult";
                            dearResult.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                            dearResult.Result = 0;
                            ProgBar.Visibility = Visibility.Visible;
                            dearResult.CurrentPage = 1;
                            dearResult.TotalPage = 0;
                            dearResult.PageSize = 20;
                            dearResult.AllowAdd = false;
                            dearResult.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(dearResult.GetPhoneInfoUrl(classid, "5", dearResult.CurrentPage)));
                            Dear.DataContext = dearResult;
                        }

                        break;

                    case 4:
                        lastPage = -1;
                        if (scoreResult == null)
                        {
                            scoreResult = new SearchResultView();
                            scoreResult.ClassName = "scoreResult";
                            scoreResult.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                            scoreResult.Result = 0;
                            ProgBar.Visibility = Visibility.Visible;
                            scoreResult.CurrentPage = 1;
                            scoreResult.TotalPage = 0;
                            scoreResult.PageSize = 20;
                            scoreResult.AllowAdd = false;
                            scoreResult.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(scoreResult.GetPhoneInfoUrl(classid, "6", scoreResult.CurrentPage)));
                            Score.DataContext = scoreResult;
                        }

                        break;

                }
            }
            else
            {
                switch (pv.SelectedIndex)
                {
                    case 0:
                        lastPage = -1;
                        keychar = "";
                        break;
                    case 1:
                        lastPage = -1;
                        keychar = "";
                        if (hotResultType == null)
                        {
                            hotResultType = new SearchResultView();
                            hotResultType.ClassName = "hotResult";
                            hotResultType.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                            hotResultType.Result = 0;
                            ProgBar.Visibility = Visibility.Visible;
                            hotResultType.CurrentPage = 1;
                            hotResultType.TotalPage = 0;
                            hotResultType.PageSize = 20;
                            hotResultType.AllowAdd = false;
                            hotResultType.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(hotResultType.GetPhoneInfoUrl(ppid, "2", hotResultType.CurrentPage, "")));
                            Hot.DataContext = hotResultType;
                        }
                        break;
                    case 2:
                        lastPage = -1;
                        keychar = "";
                        if (cheapResultType == null)
                        {
                            cheapResultType = new SearchResultView();
                            cheapResultType.ClassName = "cheapResult";
                            cheapResultType.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                            cheapResultType.Result = 0;
                            ProgBar.Visibility = Visibility.Visible;
                            cheapResultType.CurrentPage = 1;
                            cheapResultType.TotalPage = 0;
                            cheapResultType.PageSize = 20;
                            cheapResultType.AllowAdd = false;
                            cheapResultType.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(cheapResultType.GetPhoneInfoUrl(ppid, "4", cheapResultType.CurrentPage, "")));
                            Cheap.DataContext = cheapResultType;
                        }

                        break;
                    case 3:
                        lastPage = -1;
                        keychar = "";
                        if (dearResultType == null)
                        {
                            dearResultType = new SearchResultView();
                            dearResultType.ClassName = "dearResult";
                            dearResultType.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                            dearResultType.Result = 0;
                            ProgBar.Visibility = Visibility.Visible;
                            dearResultType.CurrentPage = 1;
                            dearResultType.TotalPage = 0;
                            dearResultType.PageSize = 20;
                            dearResultType.AllowAdd = false;
                            dearResultType.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(dearResultType.GetPhoneInfoUrl(ppid, "5", dearResultType.CurrentPage, "")));
                            Dear.DataContext = dearResultType;
                        }

                        break;
                    case 4:
                        lastPage = -1;
                        keychar = "";
                        if (scoreResultType == null)
                        {
                            scoreResultType = new SearchResultView();
                            scoreResultType.ClassName = "scoreResult";
                            scoreResultType.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                            scoreResultType.Result = 0;
                            ProgBar.Visibility = Visibility.Visible;
                            scoreResultType.CurrentPage = 1;
                            scoreResultType.TotalPage = 0;
                            scoreResultType.PageSize = 20;
                            scoreResultType.AllowAdd = false;
                            scoreResultType.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(scoreResultType.GetPhoneInfoUrl(ppid, "6", scoreResultType.CurrentPage, "")));
                            Score.DataContext = scoreResultType;
                        }
                        break;

                }
            }

        }
        private void verticalScrollBar_ValueChanged(object sender, RoutedEventArgs e)
        {
            //static int lastPage=-1;
            ScrollBar scrollBar = (ScrollBar)sender;
            object valueObj = scrollBar.GetValue(ScrollBar.ValueProperty);
            object maxObj = scrollBar.GetValue(ScrollBar.MaximumProperty);

            if (valueObj != null && maxObj != null)
            {
                double value = (double)valueObj;
                double max = (double)maxObj - 1.5;
                Debug.WriteLine("ValueProperty si {0}", value);
                Debug.WriteLine("MaximumProperty si {0}", max);
                if (value >= max)
                {

                    if (App.appSitting.AutoLoadNextPage == false)
                    {
                        return;
                    }

                    PivotItem currentPivot = mainPivot.SelectedItem as PivotItem;
                    if (currentPivot == null)
                    {
                        return;
                    }

                    SearchResultView infoView = currentPivot.DataContext as SearchResultView;
                    if (infoView == null)
                    {
                        return;
                    }
                    if (infoView.CurrentPage * infoView.PageSize <= infoView.TotalPage)
                    {
                        //
                        if (lastPage == infoView.CurrentPage)
                        {

                            return;
                        }
                        lastPage = infoView.CurrentPage;
                        Debug.WriteLine("add page name {0} for {1}", infoView.ClassName, infoView.CurrentPage + 1);
                        infoView.AllowAdd = true;
                        if (type == 1)
                        {
                            infoView.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(infoView.GetPhoneInfoUrl(classid, (mainPivot.SelectedIndex + 1).ToString(), infoView.CurrentPage + 1)));
                        }
                        else if (type == 2)
                        {
                            infoView.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(infoView.GetPhoneInfoUrl(ppid, (mainPivot.SelectedIndex + 1).ToString(), infoView.CurrentPage + 1, keychar)));
                        }

                    }
                }
            }
        }
        public static List<T> GetVisualChildCollection<T>(object parent) where T : UIElement
        {
            List<T> visualCollection = new List<T>();
            GetVisualChildCollection(parent as DependencyObject, visualCollection);
            return visualCollection;
        }
        private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : UIElement
        {
            int count = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < count; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(parent, i);
                if (child is T)
                {
                    visualCollection.Add(child as T);
                }
                else if (child != null)
                {
                    GetVisualChildCollection(child, visualCollection);
                }
            }
        }
        private void ListBox_Loaded(object sender, RoutedEventArgs e)
        {
            List<ScrollBar> scrollBarList = GetVisualChildCollection<ScrollBar>(sender);
            foreach (ScrollBar scrollBar in scrollBarList)
            {
                if (scrollBar.Orientation == System.Windows.Controls.Orientation.Horizontal)
                {

                }
                else
                {
                    scrollBar.ValueChanged += new RoutedPropertyChangedEventHandler<double>(verticalScrollBar_ValueChanged);
                }
            }
        }
        private void Expander_List_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (sender == null)
            {
                return;
            }
            ListBox lb = sender as ListBox;
            if (lb == null)
            {
                return;
            }

            keychar = lb.SelectedItem as string;
            SystemTray.IsVisible = true;
            KeySortPop.IsOpen = false;
            if (keychar != null && keychar.Length > 0)
            {
                switch (mainPivot.SelectedIndex)
                {
                    case 0:
                        {
                            newResultType = new SearchResultView();
                            newResultType.ClassName = "newResultType";
                            newResultType.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                            newResultType.Result = 0;
                            ProgBar.Visibility = Visibility.Visible;
                            newResultType.CurrentPage = 1;
                            newResultType.TotalPage = 0;
                            newResultType.PageSize = 20;
                            newResultType.AllowAdd = false;
                            newResultType.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(newResultType.GetPhoneInfoUrl(ppid, "1", newResultType.CurrentPage, keychar)));
                            New.DataContext = newResultType;
                        }
                        break;
                    case 1:
                        {
                            cheapResult = new SearchResultView();
                            cheapResult.ClassName = "cheapResult";
                            cheapResult.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                            cheapResult.Result = 0; ;
                            ProgBar.Visibility = Visibility.Visible;
                            cheapResult.CurrentPage = 1;
                            cheapResult.TotalPage = 0;
                            cheapResult.PageSize = 20;
                            cheapResult.AllowAdd = false;
                            cheapResult.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(cheapResult.GetPhoneInfoUrl(classid, "3", cheapResult.CurrentPage)));
                            Cheap.DataContext = cheapResult;
                        }
                        break;
                    case 2:
                        {
                            cheapResultType = new SearchResultView();
                            cheapResultType.ClassName = "cheapResult";
                            cheapResultType.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                            cheapResultType.Result = 0;
                            ProgBar.Visibility = Visibility.Visible;
                            cheapResultType.CurrentPage = 1;
                            cheapResultType.TotalPage = 0;
                            cheapResultType.PageSize = 20;
                            cheapResultType.AllowAdd = false;
                            cheapResultType.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(cheapResultType.GetPhoneInfoUrl(ppid, "3", cheapResultType.CurrentPage, keychar)));
                            Cheap.DataContext = cheapResultType;
                        }
                        break;
                    case 3:
                        {
                            dearResultType = new SearchResultView();
                            dearResultType.ClassName = "dearResult";
                            dearResultType.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                            dearResultType.Result = 0;
                            ProgBar.Visibility = Visibility.Visible;
                            dearResultType.CurrentPage = 1;
                            dearResultType.TotalPage = 0;
                            dearResultType.PageSize = 20;
                            dearResultType.AllowAdd = false;
                            dearResultType.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(dearResultType.GetPhoneInfoUrl(ppid, "4", dearResultType.CurrentPage, keychar)));
                            Dear.DataContext = dearResultType;
                        }
                        break;
                    case 4:
                        {
                            scoreResultType = new SearchResultView();
                            scoreResultType.ClassName = "scoreResult";
                            scoreResultType.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                            scoreResultType.Result = 0;
                            ProgBar.Visibility = Visibility.Visible;
                            scoreResultType.CurrentPage = 1;
                            scoreResultType.TotalPage = 0;
                            scoreResultType.PageSize = 20;
                            scoreResultType.AllowAdd = false;
                            scoreResultType.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(scoreResultType.GetPhoneInfoUrl(ppid, "4", scoreResultType.CurrentPage, keychar)));
                            Score.DataContext = scoreResultType;
                        }
                        break;
                }
            }
        }
        private ObservableCollection<string> ParseKey(string key)
        {
            if (key == null)
            {
                return null;
            }
            // Array array=new  Array();
            ObservableCollection<string> _array = new ObservableCollection<string>();
            int last = 0;//后一个逗号的位置
            int first = key.IndexOf(",", last);
            while (first > 0)
            {//
                _array.Add(key.Substring(last, first - last));
                last = first + 1;
                first = key.IndexOf(",", last);
            }
            _array.Add(key.Substring(last));
            return _array;
        }
        private void appbar_keysort(object sender, EventArgs e)
        {//
            if (sender == null)
            {
                return;
            }
            if (cheapResultTypeKyes == null || cheapResultTypeKyes.Count == 0)
            {
                MessageBox.Show("没有关键字或者关键没下载完成");
                return;
            }
            switch (mainPivot.SelectedIndex)
            {
                case 0:
                    {
                        string[] array1 = new string[newResultTypeKyes.Count];
                        newResultTypeKyes.CopyTo(array1, 0);
                        MoreListBox.DataContext = array1;
                        KeySortPop.IsOpen = true;
                        SystemTray.IsVisible = false;
                    }
                    break;
                case 1:
                    {
                        string[] array1 = new string[hotResultTypeKyes.Count];
                        hotResultTypeKyes.CopyTo(array1, 0);
                        MoreListBox.DataContext = array1;
                        KeySortPop.IsOpen = true;
                        SystemTray.IsVisible = false;
                    }
                    break;
                case 2:
                    {

                        string[] array1 = new string[cheapResultTypeKyes.Count];
                        cheapResultTypeKyes.CopyTo(array1, 0);
                        MoreListBox.DataContext = array1;
                        KeySortPop.IsOpen = true;
                        SystemTray.IsVisible = false;

                    }
                    break;
                case 3:
                    {
                        string[] array1 = new string[dearResultTypeKyes.Count];
                        dearResultTypeKyes.CopyTo(array1, 0);
                        MoreListBox.DataContext = array1;
                        KeySortPop.IsOpen = true;
                        SystemTray.IsVisible = false;
                    }
                    break;
                case 4:
                    {
                        string[] array1 = new string[scoreResultTypeKyes.Count];
                        scoreResultTypeKyes.CopyTo(array1, 0);
                        MoreListBox.DataContext = array1;
                        KeySortPop.IsOpen = true;
                        SystemTray.IsVisible = false;
                    }
                    break;
            }


        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (sender == null)
            {
                return;
            }
            KeySortPop.IsOpen = false;

        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            //这里可以执行弹出对话框操作
            if (KeySortPop.IsOpen == true)
            {
                SystemTray.IsVisible = true;
                KeySortPop.IsOpen = false;
                e.Cancel = true;
            }
        }
        private void KeySortPop_Opened(object sender, EventArgs e)
        {

        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            IDictionary<string, string> parameters = this.NavigationContext.QueryString;
            if (parameters.ContainsKey("classid"))
            {
                classid = parameters["classid"];
                type = 1;
            }
            else if (parameters.ContainsKey("ppid"))
            {
                ppid = parameters["ppid"];
                type = 2;
            }
            if (type == 1)
            {
                if (newResult == null)
                {
                    newResult = new SearchResultView();
                    newResult.ClassName = "hotResult";
                    newResult.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                    newResult.Result = 0;
                    ProgBar.Visibility = Visibility.Visible;
                    newResult.CurrentPage = 1;
                    newResult.TotalPage = 0;
                    newResult.PageSize = 20;
                    newResult.AllowAdd = false;
                    newResult.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(newResult.GetPhoneInfoUrl(classid, "1", newResult.CurrentPage)));
                    New.DataContext = newResult;
                }
            }
            else if (type == 2)
            {

                if (newResultType == null)
                {
                    newResultType = new SearchResultView();
                    newResultType.ClassName = "newResultType";
                    newResultType.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                    newResultType.Result = 0;
                    ProgBar.Visibility = Visibility.Visible;
                    newResultType.CurrentPage = 1;
                    newResultType.TotalPage = 0;
                    newResultType.PageSize = 20;
                    newResultType.AllowAdd = false;
                    newResultType.SetValue(LowProfileImageLoader.UriSourceProperty, new Uri(newResultType.GetPhoneInfoUrl(ppid, "1", newResultType.CurrentPage, "")));
                    New.DataContext = newResultType;
                }
            }

            if (NavigationContext.QueryString.ContainsKey("ispk"))
            {
                ispk = true;
                if (NavigationContext.QueryString.ContainsKey("isnull"))
                {
                    isnull = true;
                }
                else
                {
                    pid_1 = NavigationContext.QueryString["pid_1"];
                    ppid_1 = NavigationContext.QueryString["ppid_1"];
                    price_1 = NavigationContext.QueryString["price_1"];
                    picurl_1 = NavigationContext.QueryString["picurl_1"];
                    phonename_1 = NavigationContext.QueryString["phonename_1"];
                    ppname_1 = NavigationContext.QueryString["ppname_1"];
                }
            }
        }
        private void New_Loaded(object sender, RoutedEventArgs e)
        {

        }
        private void Hot_Loaded(object sender, RoutedEventArgs e)
        {
            if (type == 1)
            {

            }
            else
            {


            }
        }
        private void Cheap_Loaded(object sender, RoutedEventArgs e)
        {
            if (type == 1)
            {

            }
            else
            {


            }
        }
        private void Dear_Loaded(object sender, RoutedEventArgs e)
        {
            if (type == 1)
            {


            }
            else
            {

            }
        }
        private void Score_Loaded(object sender, RoutedEventArgs e)
        {
            if (type == 1)
            {

            }
            else
            {

            }
        }
        private void mianPage(object sender, EventArgs e)
        {
            string objective = "/Page/main.xaml";
            NavigationService.Navigate(new Uri(objective, UriKind.Relative));
        }

    }
}